import { useState, useCallback } from 'react';

import { qyyj } from '@/api';

export default function useQyyj() {
  const [yjList, setYjList] = useState<any>({});
  const getYjList = useCallback(
    async (params: { YJQF: string; enterpriseType: string }) => {
      const result = await qyyj.getYjList(params);
      if (result) {
        setYjList(result);
      } else {
        setYjList({});
      }
    },
    [],
  );
  const [rankList, setRankList] = useState<any[]>([]);
  const getRankList = useCallback(async (params: { type: string }) => {
    const result: any = await qyyj.getRankList(params);
    if (result) {
      console.log(result);
      setRankList(result);
    } else {
      setRankList([]);
    }
  }, []);

  const [marker, setMarker] = useState<any>(undefined);
  const getMarker = useCallback(async (params: { ids: string }) => {
    const result: any = await qyyj.getMarker(params);
    if (result && result.length > 0) {
      const data = result[0];
      setMarker({
        lng: data['B015602'] || null,
        lat: data['B015603'] || null,
        title: data['B102'] || null,
        id: data.id || null,
        type: data.LDCY || null,
      });
    } else {
      setMarker(undefined);
    }
  }, []);
  const removeMarker = useCallback(() => {
    setMarker(undefined);
  }, []);

  return {
    yjList,
    getYjList,
    rankList,
    getRankList,
    marker,
    getMarker,
    removeMarker,
  };
}
